package com.example.administrator.myapplication.myself;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.databinding.DataBindingUtil;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import com.android.databinding.library.baseAdapters.BR;
import com.example.administrator.myapplication.R;
import com.example.administrator.myapplication.databinding.ActivitySqliteBinding;
import com.example.administrator.myapplication.until.CommentRecyclerAdapter;
import com.example.administrator.myapplication.until.DatabaseHelper;

import java.util.ArrayList;
import java.util.List;

public class SQLiteActivity extends AppCompatActivity {
private ActivitySqliteBinding binding;
    private DatabaseHelper helper;
    private SQLiteDatabase db;
    private CommentRecyclerAdapter<SqliteEntity> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_sqlite);
        helper = new DatabaseHelper(this);
        db = helper.getReadableDatabase();
        aboutIntentExtra();
    }

    private void aboutIntentExtra() {
        String title = getIntent().getStringExtra("title");
        binding.activitySqliteTitle.setText(title);
        if(title.equals("我的关注")){
          aboutQuery();
        }
    }

    private void aboutQuery() {
        List<SqliteEntity> list = new ArrayList<>();
        Cursor cursor = db.query(DatabaseHelper.TABLE_NAME1, new String[]{"name","img","uid"}, null, null, null, null, null);
        if(cursor.moveToFirst()){
            do {
            String name = cursor.getString(cursor.getColumnIndex("name"));
            String img = cursor.getString(cursor.getColumnIndex("img"));
            String uid = cursor.getString(cursor.getColumnIndex("uid"));
            SqliteEntity entity = new SqliteEntity(name,img,uid);
            Log.d("1",entity.toString());
            list.add(entity);
            }while (cursor.moveToNext());
        }
        adapter = new CommentRecyclerAdapter<>(this,list, R.layout.sqlite_item, BR.sqliteEntity);
        binding.recycler.setAdapter(adapter);
    }
}
